package com.agricultural.iframe;

import com.agricultural.bean.Category;
import com.agricultural.bean.Product;
import com.agricultural.dbchange.CategoryDB;
import com.agricultural.dbchange.ProductDB;
import com.agricultural.service.ProductService;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Locale;

public class ChangeProduct {
    private JPanel main;
    private JLabel title;
    private JPanel body;
    private JLabel name;
    private JLabel category;
    private JLabel price;
    private JTextField inPrice;
    private JLabel description;
    private JTextField inDescription;
    private JButton submit;
    private JTextField inName;
    private JComboBox inCategory;
    private JFrame frame;
    private Product product;
    private final boolean isChange;

    public ChangeProduct(Product changeProduct) {
        this.product = changeProduct;
        initCategoryList();
        if (product != null) {
            isChange = true;
            inName.setText(product.getName());
            inPrice.setText(String.valueOf(product.getPrice()));
            inDescription.setText(product.getDescription());
            inCategory.setSelectedIndex(product.getCategoryId() - 1);
        } else {
            isChange = false;
            this.product = new Product();
        }
        initializeComponents();
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 判断各个输入框是否为空
                if (inName.getText().isEmpty() || inPrice.getText().isEmpty() || inDescription.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "请填写完整信息");
                    return;
                }
                product.setName(inName.getText());
                product.setPrice(Double.parseDouble(inPrice.getText()));
                product.setDescription(inDescription.getText());
                product.setCategoryId(CategoryDB.fromCategoryGetId((String) inCategory.getSelectedItem()));
                if (isChange) {
                    changeProduct();
                } else {
                    addProduct();
                }
            }
        });
    }

    private void initCategoryList() {
        List<Category> categoryList = CategoryDB.getCategoryList();
        for (Category category : categoryList) {
            inCategory.addItem(category.getName());
        }
    }

    private void addProduct() {
        int res = ProductDB.addProduct(product);
        if (res == 1) {
            JOptionPane.showMessageDialog(null, "添加成功");
        } else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    private void changeProduct() {
        int res = ProductDB.updateProduct(product);
        if (res == 1) {
            JOptionPane.showMessageDialog(null, "修改成功");
        } else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void initializeComponents() {
        frame = new JFrame("ChangeProduct");
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(main);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        main = new JPanel();
        main.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        title = new JLabel();
        Font titleFont = this.$$$getFont$$$(null, -1, 22, title.getFont());
        if (titleFont != null) title.setFont(titleFont);
        title.setText("商品信息修改");
        CellConstraints cc = new CellConstraints();
        main.add(title, cc.xy(1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
        body = new JPanel();
        body.setLayout(new GridLayoutManager(5, 2, new Insets(10, 10, 10, 10), -1, -1));
        main.add(body, cc.xy(1, 3));
        name = new JLabel();
        name.setText("商品名称");
        body.add(name, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        category = new JLabel();
        category.setText("分类");
        body.add(category, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        price = new JLabel();
        price.setText("价格");
        body.add(price, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inPrice = new JTextField();
        body.add(inPrice, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        description = new JLabel();
        description.setText("描述");
        body.add(description, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inDescription = new JTextField();
        body.add(inDescription, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        submit = new JButton();
        submit.setText("提交");
        body.add(submit, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inName = new JTextField();
        body.add(inName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        inCategory = new JComboBox();
        body.add(inCategory, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return main;
    }

}
